/**
 * @FileName: RadaSettingsActivity.java
 * @Package:com.bdtop.dbl.ui.activities.device
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-29
 * @version V1.0
 */

package com.bdtop.dbl.bluetooth;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.R;
import com.bdtop.dbl.utils.DBLDebug;
import com.bdtop.dbl.utils.Preferences;

/**
 * @ClassName: RadaSettingsActivity
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-29
 */
public class BleDeviceSettingsActivity extends BaseActivity {

	private static final String EXTAR_MAC = "extar_mac";
	private String mac;

	private static final int DEFAULT_JULI = 3;

	public static final void launch(Context context, String mac) {
		Intent intent = new Intent(context, BleDeviceSettingsActivity.class);
		intent.putExtra(EXTAR_MAC, mac);
		context.startActivity(intent);
		((Activity) context).overridePendingTransition(R.anim.slide_in_right, R.anim.keep_original_state);
	}

	@Override public void finish() {
		super.finish();
		overridePendingTransition(R.anim.keep_original_state, R.anim.slide_out_right);
	}

	private View layoutSettings;
	private CheckBox ckFangdiu, ckDuankai;
	private SeekBar sbJuli;

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_ble_device_setting2);
		super.onCreate(savedInstanceState);

		setTitle(R.string.radar_settings_title);
		mac = getIntent().getStringExtra(EXTAR_MAC);

		ckFangdiu = (CheckBox) findViewById(R.id.ck_fangdiu);
		layoutSettings = findViewById(R.id.layout_settings);
		ckDuankai = (CheckBox) findViewById(R.id.ck_duankai);

		sbJuli = (SeekBar) findViewById(R.id.sb_juli);

		layoutSettings.setVisibility(View.GONE);

		// 开启关闭设置，即刻生效
		ckFangdiu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (ckFangdiu.isChecked()) {
					layoutSettings.setVisibility(View.VISIBLE);
				} else {
					layoutSettings.setVisibility(View.GONE);
				}

				resetRrotectRuleAndWork();
			}
		});
		// 断开报警设置 ，即刻生效
		ckDuankai.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				resetRrotectRuleAndWork();
			}
		});

		// 距离报警设置，即刻生效
		sbJuli.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override public void onStopTrackingTouch(SeekBar seekBar) {
				resetRrotectRuleAndWork();
			}

			@Override public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			}
		});

		initProtectValue();

		bindBLeService();
	}

	private void initProtectValue() {
		boolean isFangdiu = Preferences.getBleDeviceFangdiu(this, mac);
		ckFangdiu.setChecked(isFangdiu);
		if (isFangdiu) {
			layoutSettings.setVisibility(View.VISIBLE);
		} else {
			layoutSettings.setVisibility(View.GONE);
		}
		boolean isFangdiuDuankai = Preferences.getBleDeviceFangdiuDuankai(this, mac);
		ckDuankai.setChecked(isFangdiuDuankai);

		int fangdiuJuli = Preferences.getBleDeviceFangdiuJuli(this, mac) - DEFAULT_JULI;
		sbJuli.setProgress(fangdiuJuli);

	}

	@Override protected void onDestroy() {
		super.onDestroy();

		resetRrotectRuleAndWork();

		unBindBLeService();
	}

	private void resetRrotectRuleAndWork() {
		Preferences.saveBleDeviceFangdiu(this, mac, ckFangdiu.isChecked());
		Preferences.saveBleDeviceFangdiuJuli(this, mac, sbJuli.getProgress() + DEFAULT_JULI);
		Preferences.saveBleDeviceFangdiuDuankai(this, mac, ckDuankai.isChecked());

		if (mBluetoothLeService != null) {
			mBluetoothLeService.initProtectRule();
		}
	}

	private BluetoothLeService mBluetoothLeService;

	private boolean isBindBleService = false;

	@SuppressWarnings("static-access") private void bindBLeService() {
		if (!isBindBleService) {
			isBindBleService = true;
			Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
			this.bindService(gattServiceIntent, mServiceConnection, this.BIND_AUTO_CREATE);
		}
	}

	private void unBindBLeService() {
		if (isBindBleService) {
			isBindBleService = false;
			this.unbindService(mServiceConnection);
			mBluetoothLeService = null;
		}
	}

	private final ServiceConnection mServiceConnection = new ServiceConnection() {

		@Override public void onServiceConnected(ComponentName componentName, IBinder service) {
			DBLDebug.logd("Radar Mode onServiceConnected");
			mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
		}

		@Override public void onServiceDisconnected(ComponentName componentName) {
			mBluetoothLeService = null;
		}
	};
}
